/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Core;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



/**
 *
 * @author Ghost
 */

public class Mysql {
	private Connection Con = null;
	private Statement statement = null;
	private PreparedStatement preparedStatement = null;
	private ResultSet resultSet = null;
        //Mysql connection info
        private String Host = null;
        private String User = null;
        private String Password = null; 
        private String Database = null;
        private Mysql(){
            
        }
        public Mysql(String host, String user,String pass, String database){
            Host = host; User = user; Password = pass; Database = database;
            System.out.println("Starting Mysql...");
        }
        
        public boolean Connect(){
            try{
                System.out.println("Connecting to Mysql...");
                // This will load the MySQL driver, each DB has its own driver
                Class.forName("com.mysql.jdbc.Driver");
                // Setup the connection with the DB
                Con = DriverManager.getConnection("jdbc:mysql://"+Host+"/"
                        +Database,User, Password );
                statement = Con.createStatement();
            }catch(SQLException e){
                System.err.println("SQLException:"+e);
                return false;
            }catch(ClassNotFoundException e){
                System.err.println("ClassNotFoundException:"+e);
                return false;
            }
            return true;
        }
        public boolean Query(String query){
            try{
                resultSet = statement.executeQuery(query);
                resultSet.next();
            }catch(SQLException e){
                  System.err.println("SQLException:"+e);
                  return false;
            }   
            return true;
        }
        public boolean Update(String query){
            System.err.println(query);
            try{
                statement.executeUpdate(query);
            }catch(SQLException e){
                  System.err.println("SQLException:"+e);
                  return false;
            }   
            return true;
        }
        public void setHost(String host){
            Host = host;
        }
        public void setUser(String user){
            User = user;
        }
        public void setPassword(String pass){
            Password = pass;
        }
        public void setDatabase(String database){
            Database = database;
        }
        public void Disconnect(){
            try{
                Con.close();
             }catch(SQLException e){
                System.err.println("SQLException:"+e);
            }     
        }       
}
